#
# Author: pepone.onrez@gmail.com
# License: GPL-v3
#
# This cmake script generate rules for build antlr c++ based parsers
# This script is tested with antlr-2 version as there is not yet a c++ runtime for antlr-3
#
# Assumptions all grammars are in the antlr source dir and end in .g (This is standar file extension for antlr)
# The same file has lexer and parser definitions.
# The macro permit you get the name of the generated files for the parser.
# You can use this names for build other rules.
#
# If you has any suggestions fell free to post in the user group
# http://groups.google.com/group/ydra

SET ( ANTLR_COMMAND /opt/antlr/bin/antlr )
SET ( MOVE_FILE mv -v )


# Generate rules for antlr parsers.

# This macro returns the names of the sources that's generate antlr for a given parser and generate
# a command that depends on the given parser and outputs the parser source files runing the antlr compiler.

MACRO ( GENERATE_PARSER_RULES PARSER_GENERATED_SOURCE PARSER_GENERATED_HEADER PARSER_GENERATED_TOKENS_HEADER PARSER_GENERATED_TOKENS_TXT )

    # FileName for our lexer definition
    SET ( PARSER_ANTRL_SOURCE_BASENAME ${ARGN} )

    # Full path for our lexer definition
    SET ( PARSER_ANTRL_SOURCE_FILE ${PROJECT_SOURCE_DIR}/antlr/${PARSER_ANTRL_SOURCE_BASENAME} )

    #Generate filenames based on the lexer filename
    STRING( REGEX REPLACE "\\.g" .cpp PARSER_CPP_SOURCE_BASENAME "${PARSER_ANTRL_SOURCE_BASENAME}" )
    STRING( REGEX REPLACE "\\.g" .hpp PARSER_CPP_HEADER_BASENAME "${PARSER_ANTRL_SOURCE_BASENAME}" )
    STRING( REGEX REPLACE "\\.g" TokenTypes.hpp PARSER_TOKENS_CPP_HEADER_BASENAME "${PARSER_ANTRL_SOURCE_BASENAME}" )
    STRING( REGEX REPLACE "\\.g" TokenTypes.txt PARSER_TXT_TOKENS_BASENAME "${PARSER_ANTRL_SOURCE_BASENAME}" )
    STRING( REGEX REPLACE "\\Parser.g" TokenTypes.txt LEXER_TXT_TOKENS_BASENAME "${PARSER_ANTRL_SOURCE_BASENAME}" )

    MESSAGE( STATUS "PARSER_CPP_SOURCE_BASENAME ${PARSER_CPP_SOURCE_BASENAME}" )
    MESSAGE( STATUS "PARSER_CPP_SOURCE_BASENAME ${PARSER_CPP_HEADER_BASENAME}" )
    MESSAGE( STATUS "PARSER_CPP_SOURCE_BASENAME ${PARSER_TOKENS_CPP_HEADER_BASENAME}" )
    MESSAGE( STATUS "PARSER_CPP_SOURCE_BASENAME ${PARSER_TXT_TOKENS_BASENAME}" )

    #Files generated by antlr
    SET ( PARSER_CPP_SOURCE_FILE ${PROJECT_BINARY_DIR}/${PARSER_CPP_SOURCE_BASENAME} )
    SET ( PARSER_CPP_HEADER_FILE ${PROJECT_BINARY_DIR}/${PARSER_CPP_HEADER_BASENAME} )
    SET ( PARSER_TOKENS_CPP_HEADER_FILE ${PROJECT_BINARY_DIR}/${PARSER_TOKENS_CPP_HEADER_BASENAME} )
    SET ( PARSER_TXT_TOKENS_FILE ${PROJECT_BINARY_DIR}/${PARSER_TXT_TOKENS_BASENAME} )

    #Command that runs antlr for generating the lexer code
    ADD_CUSTOM_COMMAND(
        OUTPUT ${PARSER_CPP_SOURCE_FILE} ${PARSER_CPP_HEADER_FILE} ${PARSER_TOKENS_CPP_HEADER_FILE} ${PARSER_TXT_TOKENS_FILE}
        COMMAND ${ANTLR_COMMAND} ${PARSER_ANTRL_SOURCE_FILE}
        DEPENDS ${PARSER_ANTRL_SOURCE_FILE} ${LEXER_TXT_TOKENS_FILE}
        COMMENT "-- Generating ${PARSER_CPP_SOURCE_BASENAME} ${PARSER_CPP_HEADER_BASENAME} ${PARSER_TOKENS_CPP_HEADER_BASENAME} ${PARSER_TXT_TOKENS_BASENAME} file from ${PARSER_ANTRL_SOURCE_BASENAME}")


    # Asign return values
    SET( PARSER_GENERATED_SOURCE ${PARSER_CPP_SOURCE_FILE} )
    SET( PARSER_GENERATED_HEADER ${PARSER_CPP_HEADER_FILE} )
    SET( PARSER_GENERATED_TOKENS_HEADER ${PARSER_TOKENS_CPP_HEADER_FILE} )
    SET( PARSER_GENERATED_TOKENS_TXT ${PARSER_TXT_TOKENS_FILE} )

ENDMACRO ( GENERATE_PARSER_RULES PARSER_GENERATED_SOURCE PARSER_GENERATED_HEADER PARSER_GENERATED_TOKENS_HEADER PARSER_GENERATED_TOKENS_TXT )
